package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.mapper.PermissionMapper;
import org.javaweb.cms_web.model.Enum.PermissionTypes;
import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.model.UserDTO1;
import org.javaweb.cms_web.model.UserPermission;
import org.javaweb.cms_web.service.UserMagagementService;
import org.javaweb.cms_web.service.UserService;
import org.javaweb.cms_web.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserMagagementController {
    @Autowired
    private UserMagagementService userService;
    @Resource
    private PermissionMapper permissionMapper;
    @Autowired
    private HostHolder hostHolder;

    // 显示用户管理页面，支持搜索
    @GetMapping("/admin/usermanagement")
    public String showUserManagementPage(
            @RequestParam(value = "nickname", required = false) String nickname,
            Model model) {
        User user = hostHolder.getUser();
        List<UserPermission> list=permissionMapper.getPermissionsByUserId(user.getUserId());
        if (list.stream().map(UserPermission::getPermissionType).
                noneMatch(type -> type == PermissionTypes.USER_MANAGE.value)) {
            return "nopermission";
        }
        List<UserDTO1> users = userService.findUsersForReviewAndSearch(nickname);
        model.addAttribute("users", users);
        return "user-management";  // 返回用户管理页面
    }

    // 通过用户
    @GetMapping("/admin/users/approve/{userId}")
    public String approveUser(@PathVariable("userId") Integer userId, Model model) {
        boolean success = userService.updateUserStatus(userId, 1, null);  // status = 1 表示已通过
        if (success) {
            model.addAttribute("message", "用户已通过审核");
        } else {
            model.addAttribute("message", "用户审核失败");
        }
        return "redirect:/admin/usermanagement";  // 重定向回用户管理页面
    }

    // 拒绝用户
    @GetMapping("/admin/users/reject/{userId}")
    public String rejectUser(@PathVariable("userId") Integer userId, Model model) {
        model.addAttribute("userId", userId);  // 将用户ID传递到拒绝理由表单页面
        return "user-reject";  // 跳转到拒绝理由页面
    }

    // 提交拒绝理由
    @PostMapping("/admin/users/reject")
    public String rejectUserWithReason(@RequestParam("userId") Integer userId, @RequestParam("rejectReason") String rejectReason, Model model) {
        boolean success = userService.updateUserStatus(userId, 2, rejectReason);  // status = 2 表示已拒绝
        if (success) {
            model.addAttribute("message", "用户已被拒绝");
        } else {
            model.addAttribute("message", "用户拒绝失败");
        }
        return "redirect:/admin/usermanagement";  // 重定向回用户管理页面
    }

    // 注销用户
    @PostMapping("/admin/users/deactivate/{id}")
    public String deactivateUser(@PathVariable("id") Integer userId, Model model) {
        boolean success = userService.deactivateUser(userId);
        if (success) {
            model.addAttribute("message", "用户已注销");
        } else {
            model.addAttribute("message", "用户注销失败");
        }
        return "redirect:/admin/usermanagement";  // 重定向回用户管理页面
    }

    // 重置用户密码
    @PostMapping("/admin/users/resetPassword/{id}")
    public String resetPassword(@PathVariable("id") Integer userId, Model model) {
        boolean success = userService.resetPassword(userId);
        if (success) {
            model.addAttribute("message", "密码已重置");
        } else {
            model.addAttribute("message", "密码重置失败");
        }
        return "redirect:/admin/usermanagement";  // 重定向回用户管理页面
    }
}
